home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12675 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  83 lines

  1. Path: eddie.Colorado.EDU!rangaswa
  2. From: rangaswa@eddie.Colorado.EDU (qwertyuiop)
  3. Newsgroups: comp.lang.c
  4. Subject: [Q] Experts: Multi-dim dynamic arrays
  5. Date: 2 Apr 1996 04:57:17 GMT
  6. Organization: University of Colorado,Boulder
  7. Distribution: usa
  8. Message-ID: <4jqc3d$lru@peabody.colorado.edu>
  9. NNTP-Posting-Host: eddie.colorado.edu
  10.  
  11. Hi!
  12.  
  13.     I need help with reading in integer data from a table using dynamic
  14. arrays.  For example, consider the following:
  15.  
  16. 4 4 /* number of rows, columns in the table, will vary for each data set*/
  17.  
  18. 3 5 7 8        }  This is the actual table I want read.
  19. 1 2 3 4        }
  20. 5 6 7 8        }
  21. -1 3 5 -9      }
  22.  
  23. ---------------------------------------------------------------------------
  24. Here is a synopsis of the code I tried:  I get no compilation errors, but
  25. during execution, I get floating point exception at the line marked XX.
  26. (Necessary file pointers, header files etc. are included but not shown here!.)
  27.  
  28. int **imatrix(int nrl,int nrh,int ncl,int nch);
  29. main()
  30. {
  31.  
  32. int i, j; /* loop variables */
  33. int R, C; /* number of rows, columns */
  34. int **Table; /* table of data */
  35. int X; /* scratch variable */
  36.  
  37. fscanf(fileptr, "%d %d\n", &R, &C);
  38. Table = imatrix(1,R,1,C);
  39. for(i = 0; i < R; i++)
  40.    {
  41.    for(j = 0; j < C; j++)
  42.       {
  43.     fscanf(fileptr, "%d", &X);   <------There is no need for X here, still!
  44.     *(*(A+i)+j) = X;     XX<------floating point exception occurs here!
  45.       }
  46.    fscanf(fileptr, "\n");
  47.    }
  48. ....
  49. }
  50.  
  51. int **imatrix(int nrl, int nrh, int ncl, int nch) /* see numerical recipes! */
  52. {
  53. int i, **m;
  54. m = (int **)malloc((unsigned)(nrh-nrl+1)*sizeof(int*));
  55. if(!m) nerror("alloc failure, exiting!..\n");
  56. m -= nrl;
  57.  
  58. for(i=nrl;i<=nrh;i++)
  59.    {
  60.    m[i] = (int*)malloc((unsigned)(nch-ncl+1)*sizeof(int));
  61.    if(!m[i]) nerror("alloc failure etc...\n");
  62.    m[i] -= ncl;
  63.    }
  64. return m;
  65. }
  66. ----------------------------------------------------------------------------
  67. Questions: 1. On my machine, size of int, int * are both 4 bytes. I'm using 
  68. a gnu compiler on a Unix machine.  Why do I get a floating point exception
  69. at the line mentioned above?
  70. 2. The imatrix function returns a pointer to an array of pointers to rows.
  71. I ran a quick address and content check to see if my dereferencing of pointers
  72. is faulty.  There is  a difference of 32 bytes between the address of Table[i]
  73. and Table[i+1], how come? 
  74.  
  75. Can any C guru point out what is wrong?
  76. Thanks a bunch.  Please post replies, since this may interest others too.
  77.  
  78. ~balu/..
  79.  
  80.  
  81.  
  82.  
  83.